﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MsTestUtilities.SolutionAnalysis
{
    public class Item : IItem
    {
        private Dictionary<string, string> _properties;

        public Item(CsProject parentProject, XElement element)
        {
            ParentProject = parentProject;
            Include = element.Attribute("Include").Value;
            Name = element.Name.LocalName;
            _properties = element.Descendants()
                .ToDictionary(k => k.Name.LocalName, e => e.Value);
        }

        public CsProject ParentProject { get; private set; }

        public string Include { get; private set; }

        public string Name { get; private set; }

        public string GetMetadata(string key)
        {
            return GetMetadataOrDefault(key, string.Empty);
        }

        public bool HasMetadata(string key)
        {
            return _properties.ContainsKey(key);
        }

        public string GetMetadataOrDefault(string key, string defaultValue)
        {
            if (!HasMetadata(key))
            {
                return defaultValue;
            }

            return _properties[key];
        }

        public IDictionary<string, string> Metadata
        {
            get { return _properties; }
        }
    }
}
